package com.lx.pc.restful.util;

import javax.annotation.Resource;

import com.aliyun.sms.model.SendSmsRequest;
import com.aliyun.sms.model.SendSmsResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.lx.pc.domain.vo.sms.SmsVo;
import com.lx.pc.service.sms.ISmsService;
import com.lx.pc.utils.Constants;
import com.lx.pc.utils.RandomUtils;

public class SmsMessage {
	
	@Resource(name = "smsService")
	private ISmsService smsService;

 	public static SmsVo sendSms(String phone) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", Constants.ACCESSKEY_ID, Constants.ACCESSKEY_SECRET);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", Constants.PRODUCT, Constants.DOMAIN);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest sms = new SendSmsRequest();
        //必填:待发送手机号
        sms.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        sms.setSignName("绿侠回收");
        //必填:短信模板-可在短信控制台中找到
        sms.setTemplateCode("SMS_144450236");
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        //request.setTemplateParam("您正在申请手机注册，验证码为："+rannum+"，5分钟内有效！");
        int verifyCode = RandomUtils.random();
        sms.setTemplateParam("{\"code\":\""+verifyCode+"\"}");
        
        //手机号，验证码
        SmsVo smsVo = new SmsVo();
        smsVo.setPhone(phone);
        smsVo.setCode(String.valueOf(verifyCode));
        
        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        sms.setOutId("yourOutId");

        //hint 此处可能会抛出异常，注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(sms);
        System.out.println("短信接口返回的数据----------------");
        System.out.println("Code=" + sendSmsResponse.getCode());
        System.out.println("Message=" + sendSmsResponse.getMessage());
        System.out.println("RequestId=" + sendSmsResponse.getRequestId());
        System.out.println("BizId=" + sendSmsResponse.getBizId());
        
        smsVo.setMessage(sendSmsResponse.getMessage());
        return smsVo;
    }
 	
 	public static SendSmsResponse sendSms2(String phone) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", Constants.ACCESSKEY_ID, Constants.ACCESSKEY_SECRET);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", Constants.PRODUCT, Constants.DOMAIN);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest sms = new SendSmsRequest();
        //必填:待发送手机号
        sms.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        sms.setSignName("绿侠回收");
        //必填:短信模板-可在短信控制台中找到
        sms.setTemplateCode("SMS_144450236");
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
        //request.setTemplateParam("您正在申请手机注册，验证码为："+rannum+"，5分钟内有效！");
        int verifyCode = RandomUtils.random();
        sms.setTemplateParam("{\"code\":\""+verifyCode+"\"}");
        
        //手机号，验证码
        SmsVo smsVo = new SmsVo();
        smsVo.setPhone(phone);
        smsVo.setCode(String.valueOf(verifyCode));
        
        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        sms.setOutId("yourOutId");

        //hint 此处可能会抛出异常，注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(sms);
        
        return sendSmsResponse;
    }
 	
 	
 	public static void main(String[] args) throws ClientException {
 		//SmsVo smsVo = sendSms("15121197909");
 		SendSmsResponse sendSmsResponse = sendSms2("13814884909");
 		
 		System.out.println("短信接口返回的数据----------------");
        System.out.println("Code=" + sendSmsResponse.getCode());
        System.out.println("Message=" + sendSmsResponse.getMessage());
        System.out.println("RequestId=" + sendSmsResponse.getRequestId());
        System.out.println("BizId=" + sendSmsResponse.getBizId());
 		
	}
 	

}
